Public Class frmTheoDoiNhapHangHoa
    'Dim formMain As frmMain
    Dim dataPhieuNhap As DataTable
    Dim dataChiTietPhieuNhap As DataTable
    Dim sqlQuery As String = ""
    Private Sub loadPhieuNhap(ByVal tuNgay As Date, ByVal denNgay As Date)
        sqlQuery = "SELECT nhh.ma_chungtu, nhh.ngay_chungtu, nhh.so_hoadon, nhh.ngay_hoadon, ncc.ten_nhacungcap, k.ten_kho, nhh.diengiai "
        sqlQuery += "FROM tbl_nhap_hanghoa as nhh, tbl_nhacungcap as ncc, tbl_kho as k "
        sqlQuery += "WHERE nhh.ma_nhacungcap = ncc.ma_nhacungcap "
        sqlQuery += "AND nhh.ma_kho = k.ma_kho "
        sqlQuery += "AND nhh.ngay_chungtu BETWEEN '" & Format(tuNgay, "yyyy-MM-dd") & "' AND '" & Format(denNgay, "yyyy-MM-dd") & "' "
        sqlQuery += "ORDER BY nhh.ngay_chungtu DESC"
        dataPhieuNhap = moduleDatabaseConn.GetDataTable(sqlQuery)
        Me.gridPhieuNhap.DataSource = dataPhieuNhap
        Me.lblTotal.Text = dataPhieuNhap.Rows.Count
    End Sub

    Private Sub loadChiTietPhieuNhap(ByVal ma_chungtu As String)
        sqlQuery = "SELECT ncthh.ma_hanghoa, hh.ten_bietduoc, hh.donvi_banchan, ncthh.solo, ncthh.handung, ncthh.soluong, ncthh.ngay_sanxuat, k.ten_ke "
        sqlQuery += "FROM tbl_nhap_chitiet_hanghoa as ncthh, tbl_hanghoa as hh, tbl_ke as k, tbl_nhap_hanghoa as nhh "
        sqlQuery += "WHERE ncthh.ma_chungtu = nhh.ma_chungtu "
        sqlQuery += "AND ncthh.ma_hanghoa = hh.ma_hanghoa "
        sqlQuery += "AND ncthh.ma_ke = k.ma_ke "
        sqlQuery += "AND ncthh.ma_chungtu = '" & ma_chungtu & "' "
        sqlQuery += "ORDER BY hh.ten_bietduoc"
        dataChiTietPhieuNhap = moduleDatabaseConn.GetDataTable(sqlQuery)
        Me.gridChiTietPhieuNhap.DataSource = dataChiTietPhieuNhap
        Me.lblTotalChiTiet.Text = dataChiTietPhieuNhap.Rows.Count

        sqlQuery = "SELECT thanhtien, tienvat, tongtien FROM tbl_nhap_hanghoa WHERE ma_chungtu = '" & ma_chungtu & "'"
        Dim data_temp As DataTable = moduleDatabaseConn.GetDataTable(sqlQuery)
        Try
            Me.lblThanhTien.Text = Format(CDec(data_temp.Rows.Item(0).Item(0)), "N0")
            Me.lblTongTien.Text = Format(CDec(data_temp.Rows.Item(0).Item(2)), "N0")
        Catch ex As Exception

        End Try
    End Sub
    '***********************************

    Private Sub frmTheoDoiNhapHangHoa_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        moduleUtils.setDinhDangVietNam()
        Me.txtTuNgay.Text = moduleUtils.getNgayDauThang(Today.Year, Today.Month)
        Me.txtDenNgay.Text = Today
        Me.btnDanhSach.Focus()
    End Sub

    Private Sub btnDanhSach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDanhSach.Click
        loadPhieuNhap(CDate(Me.txtTuNgay.Text.Trim), CDate(Me.txtDenNgay.Text.Trim))
    End Sub

    Private Sub gridPhieuNhap_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridPhieuNhap.CellClick
        If e.RowIndex > -1 Then
            loadChiTietPhieuNhap(Me.gridPhieuNhap.Item(0, e.RowIndex).Value)
        End If
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub DPTDenNgay_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DPTDenNgay.ValueChanged
        Me.txtDenNgay.Text = Me.DPTDenNgay.Value
    End Sub
    Private Sub DPTTuNgay_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DPTTuNgay.ValueChanged
        Me.txtTuNgay.Text = Me.DPTTuNgay.Value
    End Sub

    Private Sub gridPhieuNhap_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridPhieuNhap.CellDoubleClick
        If e.RowIndex >= 0 Then
            Dim ma_phieunhap As String = Me.gridPhieuNhap.Item(0, gridPhieuNhap.CurrentCell.RowIndex).Value
            Dim ngay_nhap As Date = CDate(Me.gridPhieuNhap.Item(1, gridPhieuNhap.CurrentCell.RowIndex).Value)

            Dim isLoaded As Boolean = False
            Dim frmNhap As New frmPhieuNhapHangHoa
            For Each frm As Form In Me.MdiParent.MdiChildren
                If frm.Name = "frmPhieuNhapHangHoa" Then
                    isLoaded = True
                    frmNhap = frm
                    Exit For
                End If
            Next

            If isLoaded Then
                frmNhap.WindowState = FormWindowState.Normal
                Me.WindowState = FormWindowState.Minimized
                frmNhap.Show()
                frmNhap.formLoad(ma_phieunhap, ngay_nhap)
            Else
                frmNhap.setLoadForm(0, ma_phieunhap, ngay_nhap)
                frmNhap.MdiParent = Me.MdiParent
                frmNhap.Show()
                Me.WindowState = FormWindowState.Minimized
            End If
        End If
    End Sub
End Class